package com.doform.android.xianggucheng.popwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.doform.android.xianggucheng.R;

/**
 * Created by Administrator on 2017/5/13.
 */

public class CommonPopWindow extends PopupWindow {

    private Context mContext;
    private int mLayoutId;
    private View mConvertView;
    private SparseArray<View> mViews;

    public CommonPopWindow(Context context, int mLayoutId) {
        super(context);
        this.mContext = context;
        this.mLayoutId = mLayoutId;
        initView();
    }


    private void initView() {
        mViews = new SparseArray<>();
        mConvertView = LayoutInflater.from(mContext).inflate(mLayoutId, null);
        mConvertView.findViewById(R.id.ll).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonPopWindow.this.dismiss();
            }
        });
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setOutsideTouchable(true);
        this.setContentView(mConvertView);
        this.setBackgroundDrawable(new ColorDrawable(0x00000000));


    }

    public <T extends View> T getView(int viewId) {

        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }


    public void backgroundAlpha(Activity activity, float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        activity.getWindow().setAttributes(lp);
    }
}
